package org.sgx.jsutil.client.appstate;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.google.gwt.thirdparty.guava.common.base.Strings;

public class AppStateParamHelper {
	
	public static String toUrl(Map<String, String> params) {
		if(params==null||params.size()==0)
			return "";
		String s = "";
		Iterator<String> it = params.keySet().iterator();
		while (it.hasNext()) {
			String k = it.next();
			s += (k + "," + params.get(k));
//			if (it.hasNext())
//				s += ",";
		}
		return s;
	}
	
	public static Map<String, String> toParams(String s) {	
//		s=s.trim(); 
//		if(s.endsWith(","))
//			s=s+","; 				
		Map<String, String> m = new HashMap<String, String>();
		try {
			String[] a = s.split(",", -1);
//			if(a.length % 2 != 0)
//				a = new String[a.length+1];			
			for (int i = 0; i < a.length; i+=2) {
				m.put(a[i], a[i+1]); 
			}
		} catch (Exception e) {
		}
		return m; 
	}

}
